from dataclasses import dataclass

from cryptography.fernet import Fernet
from colorama import Fore

from .paths import SETTING_PATH
from .utils import tip


@dataclass
class Setting(object):
    email: str
    password: str
    time_interval: int
    logistics: str
    peroration: str
    extensions: list


@tip.register('Setting', color=Fore.WHITE)
def get_setting():
    with open(SETTING_PATH, 'r', encoding='utf-8') as f:
        lines, extensions = f.readlines(), []
        email = lines[0].split(':', maxsplit=1)[1].strip()
        password = lines[1].split(':', maxsplit=1)[1].strip()
        time_interval = lines[2].split(':', maxsplit=1)[1].strip()
        logistics = lines[3].split(':', maxsplit=1)[1].strip()
        peroration = lines[4].split(':', maxsplit=1)[1].strip()

        for i in range(5, 10):
            extension_item = lines[i].split(':', maxsplit=1)[1].strip()
            if extension_item:
                extensions.append(extension_item)

    f = Fernet(b'-ggQSHeBIxeDXZINcSHfUv3Yy57Zlh-6IPrjeS8NXpI=')
    email = f.decrypt(email.encode()).decode()

    res = dict(email=email, password=password, time_interval=float(time_interval),
               logistics=logistics, peroration=peroration, extensions=extensions)

    for k, v in res.items():
        if k in ['email', 'password']:
            continue
        print(f'{k}:{v}')

    return res


CF = Setting(**get_setting())
